package com.it.controller;

import cn.dev33.satoken.util.SaResult;
import com.github.pagehelper.PageInfo;
import com.it.annotation.SystemLog;
import com.it.bean.Notice;
import com.it.dto.NoticeDTO;
import com.it.page.PageImpl;
import com.it.page.PageQuery;
import com.it.service.NoticeService;
import com.it.vo.NoticeVo;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

/**
 * @author TBH
 * @date 2025-05-16 09:31:21
 * @desc
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/notice")
public class NoticeController {

    private final NoticeService noticeService;

    @PostMapping("/page")
    @Cacheable(cacheNames = "notice", key = "T(java.util.Objects).hash(#pageQuery.page, #pageQuery.pageSize, #pageQuery.queryBody)")
    public SaResult page(@RequestBody PageQuery<NoticeDTO> pageQuery) {
        PageInfo<NoticeVo> pageInfo = noticeService.page(pageQuery);
        return SaResult.data(PageImpl.of(pageInfo));
    }


    @PostMapping("/save")
    @SystemLog(value = "站内消息", method = "消息发送")
    @CacheEvict(cacheNames = "notice", allEntries = true)
    public SaResult save(@RequestBody Notice notice) {
        noticeService.save(notice);
        return SaResult.ok();
    }

    @PostMapping("/read/{id}")
    @SystemLog(value = "站内消息", method = "消息已读")
    @CacheEvict(cacheNames = "notice", allEntries = true)
    public SaResult read(@PathVariable("id") Long id) {
        noticeService.read(id);
        return SaResult.ok();
    }
}
